package com.qs.springboot.amqp.direct;

import com.qs.springboot.util.ConnectionUtil;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import org.springframework.amqp.core.ExchangeTypes;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * 发送客户端（exchange类型为direct)
 * 发送端指定routingKey，将消息发送消息到交换机，交换机根据routingKey找到消费端绑定的消息队列，将消息入队后，由消费端获取。
 *
 * @author qinyupeng
 * @since 2019-08-05 16:05:13
 */
public class Send {

    private static final String exchange_name = "direct_exchange_test";

    public static void main(String[] args) throws IOException, TimeoutException {
        Connection connection = ConnectionUtil.getConnection();
        Channel channel = connection.createChannel();

        //声明exchange，指定类型为direct
        channel.exchangeDeclare(exchange_name, ExchangeTypes.DIRECT);
        String message = "商品新增了，id=1001";

        //发送消息，指定routingKey为insert
        channel.basicPublish(exchange_name, "insert", null, message.getBytes());
        System.out.println("[商品服务] send：" + message);

        //发送消息，指定routingKey为delete
        String message2 = "商品删除了，id=1001";
        channel.basicPublish(exchange_name, "delete", null, message2.getBytes());
        System.out.println("[商品服务] send：" + message2);

        channel.close();
        connection.close();
    }
}
